<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增数据模型')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-model-add">
            <h4 class="form-header h4">数据模型信息</h4>
            <div class="form-group">    
                <label class="col-sm-3 control-label">站点ID：</label>
                <div class="col-sm-8">
                    <input name="siteId" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">模型名称：</label>
                <div class="col-sm-8">
                    <input name="modelName" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">表格名称：</label>
                <div class="col-sm-8">
                    <input name="tableName" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">模型类别：</label>
                <div class="col-sm-8">
                    <select name="modelType" class="form-control m-b">
                        <option value="">请选择</option>
                        <option value="1">内容模型</option>
                        <option value="2">表单模型</option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">状态：</label>
                <div class="col-sm-8">
                    <div class="radio-box" th:each="dict : ${@dict.getType('sys_common_status')}">
                        <input type="radio" th:id="${'status_' + dict.dictCode}" name="status" th:value="${dict.dictValue}" th:checked="${dict.default}">
                        <label th:for="${'status_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                    </div>
                </div>
            </div>
            <h4 class="form-header h4">数据模型字段信息</h4>
            <div class="row">
                <div class="col-sm-12">
                    <button type="button" class="btn btn-white btn-sm" onclick="addColumn()"><i class="fa fa-plus"> 增加</i></button>
                    <button type="button" class="btn btn-white btn-sm" onclick="sub.delColumn()"><i class="fa fa-minus"> 删除</i></button>
                    <div class="col-sm-12 select-table table-striped">
                        <table id="bootstrap-table"></table>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "cms/model"
        var reqFlagDatas = [[${@dict.getType('sys_yes_no')}]];

        let dictFieldType = [
            {dictValue: 'INT', dictLabel: '整型'},
            {dictValue: 'BIGINT', dictLabel: '长整型'},
            {dictValue: 'DECIMAL', dictLabel: '数值行'},
            {dictValue: 'VARCHAR', dictLabel: '文本'},
            {dictValue: 'LONGTEXT', dictLabel: '长文本'},
            {dictValue: 'DATETIME', dictLabel: '日期'}
        ]

        $("#form-model-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-model-add').serialize());
            }
        }

        $(function() {
            var options = {
                pagination: false,
                showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: false,
                sidePagination: "client",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'index',
                    align: 'center',
                    title: "序号",
                    formatter: function (value, row, index) {
                    	var columnIndex = $.common.sprintf("<input type='hidden' name='index' value='%s'>", $.table.serialNumber(index));
                    	return columnIndex + $.table.serialNumber(index);
                    }
                },
                {
                    field: 'fieldCode',
                    align: 'center',
                    title: '字段编码',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='text' name='cmsModelFieldList[%s].fieldCode' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'fieldName',
                    align: 'center',
                    title: '字段名称',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='text' name='cmsModelFieldList[%s].fieldName' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'fieldType',
                    align: 'center',
                    title: '字段类别',
                    formatter: function(value, row, index) {
                        var name = $.common.sprintf("cmsModelFieldList[%s].fieldType", index);
                        return $.common.dictToSelect(dictFieldType, value, name);
                    }
                },
                {
                    field: 'fieldLength',
                    align: 'center',
                    title: '字段长度',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='text' name='cmsModelFieldList[%s].fieldLength' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'formType',
                    align: 'center',
                    title: '表单类别',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='text' name='cmsModelFieldList[%s].formType' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'reqFlag',
                    align: 'center',
                    title: '非空标识',
                    formatter: function(value, row, index) {
                        var name = $.common.sprintf("cmsModelFieldList[%s].reqFlag", index);
                        return $.common.dictToSelect(reqFlagDatas, value, name);
                    }
                },
                {
                    field: 'ord',
                    align: 'center',
                    title: '字段排序',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='text' name='cmsModelFieldList[%s].ord' value='%s'>", index, value);
                        return html;
                    }
                
                }]
            };
            $.table.init(options);
        });

        function addColumn() {
            var count = $("#" + table.options.id).bootstrapTable('getData').length;
            sub.editColumn();
        	
            $("#" + table.options.id).bootstrapTable('insertRow', {
                index: count,
                row: {
                    index: $.table.serialNumber(count),
                    fieldCode: "",
                    fieldName: "",
                    fieldType: "",
                    fieldLength: "",
                    formType: "",
                    reqFlag: "",
                    ord: ""
                }
            });
        }
    </script>
</body>
</html>